home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
BORL_TIP
/
TI1000
/
TI1779.ASC
< prev
Wrap
Text File
|
1994-02-17
|
8KB
|
397 lines
PRODUCT : Borland Pascal NUMBER : 1779
VERSION : 7.0
OS : DOS
DATE : February 17, 1994 PAGE : 1/6
TITLE : Adding and Removing Items From A Turbo Vision Listbox
{
Often projects will come along requiring the user to add or
remove items from a list of items. In Turbo Vision, the
listbox is used for presenting lists. However, there are no
built-in methods for adding or removing items from a listbox.
It is a simple matter to manipulate collection used by the
listbox. But in order for the listbox behave properly, the
range field of the listbox must be carefully updated.
The unit described below details how to add and delete items
from listbox. In this example, new items are added by entering
text in an inputline and pressing the enter key. Items are
deleted by focusing the item to be removed in the listbox and
pressing the delete key.
}
unit LBDlg;
interface
uses Objects, Views, Dialogs, Drivers, App;
type
PXDlg = ^TXDlg;
TXDlg = object(TDialog)
ItemLine: PInputLine;
ItemListBox: PListBox;
ItemList: PStringCollection;
constructor Init(var Bounds: TRect; ATitle: TTitleStr);
procedure HandleEvent(var Event: TEvent); virtual;
procedure AddItem;
procedure DelItem(var Event: TEvent);
end;
implementation
const
cmAddItem = 100;
constructor TXDlg.Init(var Bounds: TRect; ATitle: TTitleStr);
var
R: TRect;
PRODUCT : Borland Pascal NUMBER : 1779
VERSION : 7.0
OS : DOS
DATE : February 17, 1994 PAGE : 2/6
TITLE : Adding and Removing Items From A Turbo Vision Listbox
AddButton: PButton;
begin
inherited Init(Bounds, ATitle);
{ Center the dialog on screen }
Options := Options or ofCentered;
{ Insert an InputLine }
R.Assign(3, 2, 28, 3);
ItemLine := New(PInputLine, Init(R, 25));
Insert(ItemLine);
{ Insert a Listbox }
R.Assign(3, 4, 28, 9);
ItemLIstBox := New(PListbox, Init(R, 1, nil));
Insert(ItemListBox);
{ Inserting a Add Button }
{ The Add Button will be a default button able to respond to
the Enter key.
}
R.Assign(29, 2, 36, 4);
AddButton := New(PButton, Init(R, '~A~dd', cmAddItem,
bfDefault));
Insert(AddButton);
{ Hide the Add button }
{ This step is need only if you do not wish the Add button to
appear in the dialog.
}
AddButton^.Hide;
{ Set the input focus to the Inputline }
ItemLine^.Focus;
{ Create an empty string collection for the listbox }
ItemList := New(PStringCollection, Init(5, 1));
ItemListBox^.NewList(ItemList);
end;
procedure TXDlg.HandleEvent(var Event: TEvent);
begin
PRODUCT : Borland Pascal NUMBER : 1779
VERSION : 7.0
OS : DOS
DATE : February 17, 1994 PAGE : 3/6
TITLE : Adding and Removing Items From A Turbo Vision Listbox
{ Check for the Delete key }
{ This is done before the inherited HandleEvent as TDialog's
HandleEvent will normally pass keystrokes on to the focused
control.
}
case Event.What of
evKeyDown:
case Event.KeyCode of
kbDel: DelItem(Event);
end;
end;
{ Process events normally }
inherited HandleEvent(Event);
{ Check to see if the Add button is pressed }
case Event.What of
evCommand:
case Event.Command of
cmAddItem: AddItem;
end;
end;
end;
procedure TXDlg.AddItem;
begin
if ItemLine^.Data^ <> '' then
begin
{ Insert the InputLine's Data into the collection used by
the listbox.
}
ItemList^.Insert(NewStr(ItemLine^.Data^));
{ Adjust the range of valid values seen by the listbox }
ItemListBox^.SetRange(ItemList^.Count);
{ Clear out the Inputline }
ItemLine^.Data^ := ''; ItemLine^.CurPos := 0;
{ Force the dialog to redraw itself }
ReDraw;
end;
PRODUCT : Borland Pascal NUMBER : 1779
VERSION : 7.0
OS : DOS
DATE : February 17, 1994 PAGE : 4/6
TITLE : Adding and Removing Items From A Turbo Vision Listbox
end;
procedure TXDlg.DelItem(var Event: TEvent);
begin
{ If the Inputline is not focused and there is something in
listbox, remove the focused item from the list used by the
listbox.
}
if (Current <> PView(ItemLine)) and (ItemList^.Count <> 0)
then
begin
{ Clear the event as the DEL is being handled here }
ClearEvent(Event);
{ Release the memory used by the focused item in the
listbox
}
ItemList^.AtFree(ItemListBox^.Focused);
{ Reset the range of values seen by the listbox }
ItemListBox^.SetRange(ItemList^.Count);
{ Adjust the focused item in the listbox if necessary }
if(ItemListBox^.Range > 0) and
(ItemListBox^.Focused > ItemListBox^.Range-1)
then
ItemListbox^.Focused := ItemListBox^.Range-1;
{ if the Listbox is empty, focus the inputline }
{ This step is entirely optional }
if ItemListbox^.Range = 0 then SelectNext(True);
{ For the dialog to redraw itself }
ReDraw;
end;
end;
end.
{ Here is a small program to that uses the dialog described
above.
}
PRODUCT : Borland Pascal NUMBER : 1779
VERSION : 7.0
OS : DOS
DATE : February 17, 1994 PAGE : 5/6
TITLE : Adding and Removing Items From A Turbo Vision Listbox
program LBAdd;
uses
LBDlg,
Objects, Views, Dialogs, Memory, Drivers, Menus, StdDlg,
ColorSel, App;
const
cmAction = 103;
type
PMainApp = ^TMainApp;
TMainApp = object(TApplication)
procedure InitMenuBar; virtual;
procedure HandleEvent(var Event: TEvent); virtual;
procedure Action;
end;
procedure TMainApp.InitMenuBar;
var
R: TRect;
begin
GetExtent(R);
R.B.Y := R.A.Y + 1;
MenuBar := New(PMenuBar, Init(R, NewMenu(
NewItem('~A~ction', '', kbNoKey, cmAction, hcNoContext,
nil)
)));
end;
procedure TMainApp.HandleEvent(var Event: TEvent);
begin
inherited HandleEvent(Event);
case Event.What of
evCommand:
case Event.Command of
cmAction: Action;
end;
end;
end;
procedure TMainApp.Action;
var
PRODUCT : Borland Pascal NUMBER : 1779
VERSION : 7.0
OS : DOS
DATE : February 17, 1994 PAGE : 6/6
TITLE : Adding and Removing Items From A Turbo Vision Listbox
R: TRect;
Dlg: PXDlg;
begin
R.Assign(0, 0, 40, 12);
New(Dlg, Init(R, 'Action Dialog'));
Application^.ExecuteDialog(Dlg, nil);
end;
var
MainApp: TMainApp;
begin
MainApp.Init;
MainApp.Run;
MainApp.Done
end.
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.